<?php
/**
 *  列表文章模型类
 */
namespace app\admin\model;
use think\Model;
use think\Db;
class Content extends Model
{

    protected $scodes = array();

    // 获取文章列表
    public function getList($UrlParam,$PageCount)
    {
		$Where = array();
        $field = array(
            'a.id',
            'b.name as sortname',
            'a.scode',
            'c.name as subsortname',
            'a.subscode',
            'a.title',
            'a.subtitle',
            'a.date',
            'a.sorting',
			'a.lkid',
            'a.status',
            'a.istop',
            'a.isrecommend',
            'a.isheadline',
			'a.isslide',
            'a.visits',
            'a.ico',
            'a.pics',
            'a.filename',
            'a.outlink',
            'd.urlname',
			'd.type',
			'b.lkcode',
			'b.mcode',
            'b.filename as sortfilename'
        );
		//$where['d.type']=2;
		if(!empty($UrlParam['mcode'])) $where['b.mcode']=$UrlParam['mcode'];
		//if(!empty($UrlParam['lkid'])) $Where['a.lkid']=$UrlParam['lkid'];
        return Db::name('content')
			->field($field)
			->alias('a')
            ->where($where)
            ->join('content_sort b', 'a.scode=b.scode', 'left')
			->join('content_sort c', 'a.scode=c.scode', 'left')
			->join('model d', 'b.mcode=d.mcode', 'left')
            ->order('a.sorting ASC,a.id DESC')
			->paginate($PageCount,false,['query' => $UrlParam])
			->each(function($item, $key){
				if($item['type']!=1){
					$item['link']=url('../index/'.$item['urlname'],['id'=>$item['id']]);
				}else{
					$item['link']=url('../index/'.$item['urlname'],['tid'=>$item['scode']]);
				}
				return $item;
			});

    }

    // 查找指定分类及子类文章
    public function findContent($UrlParam,$PageCount)
    {
		$Where = array();
        $fields = array(
            'a.id',
            'b.name as sortname',
            'a.scode',
            'c.name as subsortname',
            'a.subscode',
            'a.title',
			'a.lkid',
            'a.subtitle',
            'a.date',
            'a.sorting',
            'a.status',
            'a.istop',
            'a.isrecommend',
            'a.isheadline',
			'a.isslide',
            'a.visits',
            'a.ico',
            'a.pics',
            'a.filename',
            'a.outlink',
            'd.urlname',
			'd.type',
			'b.lkcode',
			'b.mcode',
            'b.filename as sortfilename'
        );
        $this->scodes = array(); // 先清空
        $scodes = $this->getSubScodes($UrlParam['scode']);

		if(!empty($UrlParam['mcode'])) $Where['b.mcode']=$UrlParam['mcode'];
		//if(!empty($UrlParam['lkid'])) $Where['a.lkid']=$UrlParam['lkid'];
		if(!empty($UrlParam['keyword'])) $Where['a.title']=['like','%'.trim($UrlParam['keyword']).'%'];
		if(!empty($scodes)) $Where['a.scode']=['in',$scodes];

		//var_dump($scodes);
		//die;

        return $this->field($fields)
			->alias('a')
			//->where("a.scode",array('in', $scodes))
			->where($Where)
            ->join('content_sort b', 'a.scode=b.scode', 'left')
			->join('content_sort c', 'a.scode=c.scode', 'left')
			->join('model d', 'b.mcode=d.mcode', 'left')
			->paginate($PageCount,false,['query' => $UrlParam])
			->each(function($item, $key){
				if($item['type']!=1){
					$item['link']=url('../index/'.$item['urlname'],['id'=>$item['id']]);
				}else{
					$item['link']=url('../index/'.$item['urlname'],['tid'=>$item['scode']]);
				}
				return $item;
			});
    }

    // 在全部栏目查找文章
    public function findContentAll($UrlParam,$PageCount)
    {
		$Where = array();
        $fields = array(
            'a.id',
            'b.name as sortname',
            'a.scode',
            'c.name as subsortname',
            'a.subscode',
            'a.title',
			'a.lkid',
            'a.subtitle',
            'a.date',
            'a.sorting',
            'a.status',
            'a.istop',
            'a.isrecommend',
            'a.isheadline',
			'a.isslide',
            'a.visits',
            'a.ico',
            'a.pics',
            'a.filename',
            'a.outlink',
            'd.urlname',
			'd.type',
			'b.lkcode',
			'b.mcode',
            'b.filename as sortfilename'
        );
		if(!empty($UrlParam['mcode'])) $Where['b.mcode']=$UrlParam['mcode'];
		if(!empty($UrlParam['keyword'])) $Where['a.title']=['like','%'.trim($UrlParam['keyword']).'%'];
        return $this->alias('a')
			->field($fields)
            ->where($Where)
            //->where('d.type=2 OR d.type is null ')
			->join('content_sort b', 'a.scode=b.scode', 'left')
			->join('content_sort c', 'a.scode=c.scode', 'left')
			->join('model d', 'b.mcode=d.mcode', 'left')
            ->order('a.sorting ASC,a.id DESC')
            ->paginate($PageCount,false,['query' => $UrlParam])
			->each(function($item, $key){
				if($item['type']!=1){
					$item['link']=url('../index/'.$item['urlname'],['id'=>$item['id']]);
				}else{
					$item['link']=url('../index/'.$item['urlname'],['tid'=>$item['scode']]);
				}
				return $item;
			});
    }

    // 获取子栏目
    public function getSubScodes($scode)
    {
        if (! $scode) {
            return;
        }
        $this->scodes[] = $scode;
        $subs = Db::name('content_sort')->where("pcode='$scode'")->column('scode');
        if ($subs) {
            foreach ($subs as $value) {
                $this->getSubScodes($value);
            }
        }
        return $this->scodes;
    }

    // 检查文章
    public function checkContent($where)
    {
        return $this->field('id')
            ->where($where)
            ->find();
    }

    // 获取文章详情
    public function getContent($id)
    {
        $field = array(
            'a.*',
            'b.name as sortname',
            'c.name as subsortname',
			'd.*'
        );
        return $this->alias('a')
			->field($field)
            ->where("a.id=$id")
            ->join('content_sort b', 'a.scode=b.scode', 'left')
			->join('content_sort c', 'a.subscode=c.scode', 'left')
			->join('content_ext d', 'a.id=d.contentid', 'left')
            ->find();
    }

    // 添加文章
    public function addContent(array $data)
    {
        return $this->insertGetId($data);
    }

    // 删除文章
    public function delContent($id)
    {
        return $this->where("id=$id")
            ->delete();
    }

    // 删除文章
    public function delContentList($ids)
    {
		$where['id']=['in',$ids];
        return $this->where($where)->delete();
    }

    // 修改文章
    public function modContent($id, $data)
    {
		$where['id']=['=',$id];
        $result=$this->where($where)->update($data);
		return  $result;
    }

    // 复制内容到指定栏目
    public function copyContent($ids, $scode)
    {
        // 查找出要复制的主内容
		$where['id']=['in',$ids];
        $data =Db::name('content')
			->where($where)
			->select();
		foreach ($data as $key => $value) {
            // 查找扩展内容
            $extdata = Db::name('content_ext')->where('contentid=' . $value['id'])->find(1);
            // 去除主键并修改栏目
            unset($value['id']);
            $value['scode'] = $scode;
            // 插入主内容
            $id = $this->insertGetId($value);
            // 插入扩展内容
            if ($id && $extdata) {
                unset($extdata['extid']);
                $extdata['contentid'] = $id;
                $result = Db::name('content_ext')->insertGetId($extdata);
            } else {
                $result = $id;
            }
        }
         return $result;
    }

    // 查找文章扩展内容
    public function findContentExt($id)
    {
        return Db::name('content_ext')->where("contentid=$id")->find();
    }

    // 添加文章扩展内容
    public function addContentExt(array $data)
    {
        return Db::name('content_ext')->insert($data);
    }

    // 修改文章扩展内容
    public function modContentExt($id, $data)
    {
        return Db::name('content_ext')->where("contentid=$id")->update($data);
    }

    // 删除文章扩展内容
    public function delContentExt($id)
    {
        return Db::name('content_ext')->where("contentid=$id")->delete();
    }

    // 删除文章扩展内容
    public function delContentExtList($ids)
    {
        return Db::name('content_ext')->delete($ids, 'contentid');
    }

    // 检查自定义URL名称
    public function checkFilename($filename, $where = array())
    {
        return $this->field('id')
            ->where("filename='$filename'")
            ->where($where)
            ->find();
    }
}